import { BaseCommand, TSelectRanage, TMergeCell } from '../types/common.interface';
import { TableDatas } from '../cors/table-datas';
import { Config } from '../cors/config';
import { RenderTrigger } from '../cors/render-trigger';
import { Row } from '../cors/row';
import { objTransformToClass, classTransformToObj } from '../decorators/toJSON';
import { MergeCell } from '../cors/merge-cell';
import { Select } from '../cors/select';

export class DeleteRowCommand extends BaseCommand {
    protected past: any[] = [];
    protected pastConf: any = {};
    protected pastSelect: any = null;
    protected pastMergeCell: any = {};
    constructor(
        protected tableDatas: TableDatas,
        protected renderTrigger: RenderTrigger,
        protected conf: Config,
        protected mergeCell: MergeCell,
        protected select: Select,
        protected rows: number[],
    ) {
        super();
    }
    public execute(): boolean {
        this.pastConf = classTransformToObj(this.conf);
        this.pastMergeCell = classTransformToObj(this.mergeCell);
        this.pastSelect = classTransformToObj(this.select);
        this.past = this.tableDatas.deleteRows(this.rows);
        this.renderTrigger.triggerRenderAll();        
        return true;
    }
    public addRows(rows: {row: number;[key: string]: any;}[]) {
        for(let rowObj of rows) {
            if(Object.keys(rowObj).length === 1) {
                if(this.tableDatas.getMaxRow() < rowObj.row) {
                    
                } else {
                    // let rowInstance = this.factory(Row);
                    this.tableDatas.rows.splice(rowObj.row, 0, null);
                }
            } else {
                let rowInstance = this.tableDatas.factory(Row);
                objTransformToClass(rowInstance, rowObj);
                if(this.tableDatas.getMaxRow() > rowObj.row) {
                    this.tableDatas.rows.splice(rowObj.row, 0, rowInstance);
                } else {
                    this.tableDatas.rows[rowObj.row] = rowInstance;
                }
            }
        }
    }
    public factory<T>(args: any): T {
        return null;
    }
    public redo(): boolean {
        objTransformToClass(this.conf, this.pastConf);
        objTransformToClass(this.mergeCell, this.pastMergeCell);
        objTransformToClass(this.select, this.pastSelect);
        this.addRows(this.past);
        this.renderTrigger.triggerRenderAll();
        return true;
    }
}